package com.teach.javafxclient.controller.base;

import com.teach.javafxclient.Globalvar;
import com.teach.javafxclient.MainApplication;
import com.teach.javafxclient.request.HttpRequestUtil;
import com.teach.javafxclient.util.HttpUtil;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import kong.unirest.HttpResponse;
import kong.unirest.JsonNode;
import kong.unirest.Unirest;
import kong.unirest.json.JSONArray;
import kong.unirest.json.JSONObject;

import java.io.IOException;

/**
 * @author Liang Jianlin
 * @date 2024-04-30 02:31
 */
public class CourseManageController {
    @FXML
    public VBox studentContainer;
    public Pane addStudent;
    @FXML
    public Pane scoreUpload;
    public Label studentNum;
    @FXML
    public Button deleteIt;
    @FXML
    public Button finishIt;
    @FXML
    public Label outputLabel;
    @FXML
    public Button showMore;
    @FXML
    public VBox subContent;
    String courseId;

    public void initialize(){
        scoreUpload.setOnMouseClicked(e->{
            FXMLLoader fxmlLoader = new FXMLLoader(MainApplication.class.getResource("base/score-entry.fxml"));
            Parent root;
            try {
                root = fxmlLoader.load();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            ScoreEntryController SEC = fxmlLoader.getController();
            Stage stage = new Stage();
            try {
                SEC.set(courseId);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            SEC.exit.setOnAction(e2->{
                stage.close();
            });
            Scene scene = new Scene(root, 600.0, 400.0);
            stage.setTitle("录入成绩");
            stage.setScene(scene);
            stage.show();
            stage.setOnCloseRequest(event -> {
                HttpRequestUtil.close();
            });

        });
        showMore.setOnMouseClicked(e->{
            subContent.setVisible(!subContent.isVisible());
        });
    }
    public void set(String courseId) throws IOException {
        addStudent.setOnMouseClicked(e-> {
            FXMLLoader fxmlLoader = new FXMLLoader(MainApplication.class.getResource("base/student-select.fxml"));
            Parent root;
            try {
                root = fxmlLoader.load();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            StudentSelectController SSC = fxmlLoader.getController();
            SSC.confirm.setOnAction(e1->{
                HttpResponse res =  HttpUtil.post(Globalvar.baseUrl+"/api/courses/"+courseId+"/enroll")
                        .header("Content-Type", "application/json;charset=UTF-8")
                        .body(SSC.getSelectedStudentList())
                        .asJson();
                if(res.getStatus() == 200){
                    SSC.output.setText("选课成功");
                }else{
                    SSC.output.setText("选课失败");
                }
            });
            Stage stage = new Stage();
            Scene scene = new Scene(root, 500.0, 400.0);
            stage.setTitle("选择学生");
            stage.setScene(scene);
            stage.show();
            stage.setOnCloseRequest(event -> {
                HttpRequestUtil.close();
            });
        });
        this.courseId = courseId;
        JSONArray res = HttpUtil.get(Globalvar.baseUrl+"/api/courses/"+courseId+"/scores")
                .header("Accept", "application/json")
                .asJson()
                .getBody()
                .getArray();
        studentNum.setText("("+res.length()+"人)");

        deleteIt.setOnAction(e->{
            HttpResponse deleteRes = HttpUtil.delete(Globalvar.baseUrl+"/api/courses/"+courseId)
                    .header("Accept", "application/json")
                    .asJson();
            if(deleteRes.getStatus() == 200){
                outputLabel.setText("成功删除课程");
            }else{
                outputLabel.setText("删除课程失败");
            }
        });
        finishIt.setOnAction(e->{
            HttpResponse finishRes = HttpUtil.post(Globalvar.baseUrl+"/api/courses/"+courseId+"/end")
                    .header("Accept", "application/json")
                    .asJson();
            if(finishRes.getStatus() == 200){
                outputLabel.setText("结课成功");
            }else{
                outputLabel.setText("结课失败");
            }
        });

        for (int i = 0; i < res.length(); i++) {
            JSONObject JO = (JSONObject) res.get(i);
            JSONObject student = ((JSONObject)res.get(i)).getJSONObject("student");
            FXMLLoader fxmlLoader = new FXMLLoader(MainApplication.class.getResource("base/mini-student-item.fxml"));
            Parent root = fxmlLoader.load();
            MiniStudentItemController MSIC = fxmlLoader.getController();
            MSIC.name.setText((String) student.get("name"));
            MSIC.id.setText(String.valueOf(student.get("studentId")));
            if(!JO.isNull("score")){
                MSIC.scoreContainer.setVisible(true);
                MSIC.score.setText(String.valueOf(JO.get("score")));
                MSIC.main.setStyle("-fx-background-color: #f4f9ff");
            }else if(!JO.isNull("grade")){
                MSIC.scoreContainer.setVisible(true);
                MSIC.score.setText(String.valueOf(JO.get("grade")));
                MSIC.main.setStyle("-fx-background-color: #f4f9ff");
            }else {
                MSIC.nonScore.setVisible(true);
                MSIC.main.setStyle("-fx-background-color: #fff9fd");
            }
            studentContainer.getChildren().add(root);
        }
    }
}
